package com.xinsoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.entity.dto.MesProcessPlanDetailDTO;
import com.xinsoft.entity.po.*;
import com.xinsoft.service.MesDocumentService;
import com.xinsoft.service.MesProcessDetailDeviceRefService;
import com.xinsoft.service.MesProcessPlanDetailService;
import com.xinsoft.mapper.MesProcessPlanDetailMapper;
import com.xinsoft.service.SysFileHisService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author hsm
* @description 针对表【mes_process_plan_detail(工序计划明细)】的数据库操作Service实现
* @createDate 2023-09-13 15:28:29
*/
@Service
public class MesProcessPlanDetailServiceImpl extends ServiceImpl<MesProcessPlanDetailMapper, MesProcessPlanDetail>
    implements MesProcessPlanDetailService{

    @Resource
    public MesProcessDetailDeviceRefService mesProcessDetailDeviceRefService;

    @Resource
    public MesProcessDetailTeamUserRefServiceImpl mesProcessDetailTeamUserRefServiceImpl;

    @Resource
    public MesDocumentService mesDocumentService;

    @Autowired
    private SysFileHisService fileHisService;


    @Override
    public MesProcessPlanDetail getDetailBySerial(HashMap param) {
        return this.baseMapper.getDetailBySerial(param);
    }

    @Override
    public List<MesProcessPlanDetailDTO> getMesProcessPlanDetailListByPlanId(Integer planId) {

        List<MesProcessPlanDetailDTO> detailDTOList = new ArrayList<>();


        QueryWrapper query = new QueryWrapper<>();
        query.eq("process_plan_id",planId);
        List<MesProcessPlanDetail> details = list(query);

        for (MesProcessPlanDetail detail : details) {
            MesProcessPlanDetailDTO detailDTO = new MesProcessPlanDetailDTO();
            BeanUtils.copyProperties(detail,detailDTO);

             // 设备组
             List<MesProcessDetailDeviceRef> mesProcessDetailDeviceRefList = mesProcessDetailDeviceRefService
                     .list(new QueryWrapper<MesProcessDetailDeviceRef>().eq("process_plan_detail_id",detail.getId()));
            detailDTO.setMesProcessDetailDeviceRefList(mesProcessDetailDeviceRefList);
            //班组
             List<MesProcessDetailTeamUserRef> MesProcessDetailTeamUserRefList = mesProcessDetailTeamUserRefServiceImpl
                     .list(new QueryWrapper<MesProcessDetailTeamUserRef>().eq("process_plan_detail_id",detail.getId()));
            detailDTO.setMesProcessDetailTeamUserRefList(MesProcessDetailTeamUserRefList);

            // 追加sop
            List<MesDocument> documentList = mesDocumentService.list(new QueryWrapper<MesDocument>()
                    .eq("mes_type",2)
                    .eq("mes_id", detail.getId()));
            List<Integer> fileIds = new ArrayList<>();
            for (MesDocument mesDocument : documentList) {
                fileIds.add(mesDocument.getDocumentId());
            }
            if (documentList.size() > 0) {
                List<SysFileHis> sysFileHis = fileHisService.listByIds(fileIds);
                detailDTO.setSysFileHisList(sysFileHis);
            }
            detailDTOList.add(detailDTO);
        }

        return detailDTOList;
    }

    @Override
    public void urgent(Integer detailId) {
        MesProcessPlanDetail entity = this.getById(detailId);
        List<MesProcessPlanDetail> list = list(new QueryWrapper<MesProcessPlanDetail>().eq("process_plan_id",entity.getProcessPlanId()).orderByAsc("serial_number"));
        int serialNumber = 1;
        for (MesProcessPlanDetail detail : list) {
            if(detail.getId().equals(detailId)){
                detail.setSerialNumber(1);
            }else{
                detail.setSerialNumber(serialNumber);
                serialNumber++;
            }
        }
        updateBatchById(list);
    }

    @Override
    public void edit(MesProcessPlanDetailDTO detailDTO) {
        //更新detail
        MesProcessPlanDetail detail = new MesProcessPlanDetail();
        BeanUtils.copyProperties(detailDTO,detail);
        updateById(detail);
        // 设备组
        QueryWrapper deviceRefQuery = new QueryWrapper<>();
        deviceRefQuery.eq("process_plan_detail_id",detailDTO.getId());
        mesProcessDetailDeviceRefService.remove(deviceRefQuery);

        // 班组
        QueryWrapper teamRefQuery = new QueryWrapper<>();
        teamRefQuery.eq("process_plan_detail_id",detailDTO.getId());
        mesProcessDetailTeamUserRefServiceImpl.remove(teamRefQuery);
        if(!CollectionUtils.isEmpty(detailDTO.getMesProcessDetailDeviceRefList())){
            for (MesProcessDetailDeviceRef mesProcessDetailDeviceRef : detailDTO.getMesProcessDetailDeviceRefList()) {
                mesProcessDetailDeviceRef.setId(null);
                mesProcessDetailDeviceRef.setProcessPlanDetailId(detailDTO.getId());
            }
            mesProcessDetailDeviceRefService.saveBatch(detailDTO.getMesProcessDetailDeviceRefList());
        }

        if(!CollectionUtils.isEmpty(detailDTO.getMesProcessDetailTeamUserRefList())){
            for (MesProcessDetailTeamUserRef mesProcessDetailTeamUserRef : detailDTO.getMesProcessDetailTeamUserRefList()) {
                mesProcessDetailTeamUserRef.setId(null);
                mesProcessDetailTeamUserRef.setProcessPlanDetailId(detailDTO.getId());
            }

            mesProcessDetailTeamUserRefServiceImpl.saveBatch(detailDTO.getMesProcessDetailTeamUserRefList());
        }
    }
}




